const fs = require('fs')
const path = require('path')

function getFileMime(extname){
    // 使用同步方法返回数据
    var data = fs.readFileSync('./处理文件后缀名/mime.json');
    let mimeobj=JSON.parse(data.toString());
    return mimeobj[extname];
}

exports.static = function (req,res,staticPath){
    let pathname = req.url;
    // console.log(pathname);
    //获取文件后缀名
    const extname = path.extname(pathname)

    if(pathname != '/favicon.ico'){
        try{
            let data = fs.readFileSync('.'+pathname)
            if(data){
                let mime =getFileMime(extname)
                res.writeHead(200, {'Content-Type': `${mime};charset=utf-8`});
                res.end(data)
                return true
            }
        }catch(err){
            return false;
        }   
    }
}